﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYElectricKernel;
using HYElectricKernel.Generics;

namespace HYRElectric.LightningCalculation.Forms
{
	public partial class GraphicMethodSubForm : System.Windows.Forms.Form
	{
		public GraphicMethodSubForm(Document doc)
		{
			this.m_Doc = doc;
			this.InitializeComponent();
			this.btnAdd.Image = Resource.GetImage("AddNew16");
			this.btnEdit.Image = Resource.GetImage("modify16");
			this.btnDelete.Image = Resource.GetImage("delete16");
		}

		public List<Level> GetSelectLevels()
		{
			List<Level> list = new List<Level>();
			foreach (object obj in this.ElementslistView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				list.Add(listViewItem.Tag as Level);
			}
			return list;
		}

		public void ClearSelectLevels()
		{
			this.ElementslistView.BeginUpdate();
			this.ElementslistView.Items.Clear();
			this.ElementslistView.EndUpdate();
		}

		private void GraphicMethodSubForm_Load(object sender, EventArgs e)
		{
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAdd, "添加");
			toolTip.SetToolTip(this.btnEdit, "编辑");
			toolTip.SetToolTip(this.btnDelete, "删除");
			List<Level> levelList = new List<Level>();
			this.InitializeLevelListView(levelList);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			List<Level> selectLevels = this.GetSelectLevels();
			LevelListForm levelListForm = new LevelListForm(this.m_Doc, selectLevels);
			if (levelListForm.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			List<Level> addLevels = levelListForm.GetAddLevels();
			selectLevels.AddRange(addLevels);
			selectLevels.Sort(new LevelComparer());
			this.InitializeLevelListView(selectLevels);
		}

		private void InitializeLevelListView(List<Level> levelList)
		{
			this.ElementslistView.BeginUpdate();
			this.ElementslistView.Items.Clear();
			foreach (Level level in levelList)
			{
				double num = Common.FeetToMM(level.Elevation) / 1000.0;
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.Tag = level;
				listViewItem.SubItems.Add(level.Name);
				listViewItem.SubItems.Add(num.ToString("f0"));
				this.ElementslistView.Items.Add(listViewItem);
			}
			this.ElementslistView.EndUpdate();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("确认要删除数据？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
			{
				return;
			}
			this.ElementslistView.BeginUpdate();
			foreach (object obj in this.ElementslistView.SelectedItems)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				this.ElementslistView.Items.RemoveAt(listViewItem.Index);
			}
			this.ElementslistView.EndUpdate();
		}

		private Document m_Doc;
	}
}
